package com.ds.lens.data.service.dao;

import com.ds.lens.data.service.dao.entity.LensMetricsTags;
import com.ds.lens.data.service.dao.sql.provider.LensMetricsTagsSqlProvider;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Description:
 *
 * @author hill
 * @date 2019-11-05
 */

@Repository
public interface LensMetricsTagsDao {

    String COLUMNS = " id, metric_name,app_id, app_key, tag_key, tag_value, tag_full  ";



    @Insert({
            "insert into lens_metrics_tags (",
            COLUMNS,
            " ) values (#{id,jdbcType=BIGINT}, #{metric_name,jdbcType=VARCHAR}, #{appId,jdbcType=BIGINT},",
            "#{appKey,jdbcType=VARCHAR}, #{tag_key,jdbcType=VARCHAR}, ",
            "#{tag_value,jdbcType=VARCHAR}, #{tag_full,jdbcType=VARCHAR}"

    })
    int insert(LensMetricsTags record);

    @InsertProvider(type = LensMetricsTagsSqlProvider.class, method = "insertSelective")
    int insertSelective(LensMetricsTags record);

    @Select({
            "select",
            COLUMNS,
            "from lens_metrics_tags",
            "where id = #{id,jdbcType=BIGINT}"
    })
    @Results(id = "lensMetricsTags", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "metric_name", property = "metricName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "app_id", property = "appId", jdbcType = JdbcType.BIGINT),
            @Result(column = "app_key", property = "appKey", jdbcType = JdbcType.VARCHAR),
            @Result(column = "tag_key", property = "tagKey", jdbcType = JdbcType.VARCHAR),
            @Result(column = "tag_value", property = "tagValue", jdbcType = JdbcType.VARCHAR),
            @Result(column = "tag_full", property = "tagFull", jdbcType = JdbcType.VARCHAR)

    })
    LensMetricsTags selectByPrimaryKey(Long id);

    @Select({
            "select",
            COLUMNS,
            "from lens_metrics_tags"
    })
    @ResultMap("lensMetricsTags")
    List<LensMetricsTags> selectAllMetricsTags();

    @Insert("<script> insert into lens_metrics_tags(metric_name, app_id , app_key, tag_key, tag_value, tag_full)  values" +
            " <if test='records != null and records.size > 0'> " +
            "<foreach collection='records' item='__item' separator=',' >(#{__item.metricName},#{__item.appId},#{__item.appKey},#{__item.tagKey},#{__item.tagValue},#{__item.tagFull})</foreach>" +
            " </if>" +
            "</script>")
    int batchInsert( @Param("records") List<LensMetricsTags> records);

    @Select({
            "select",
            " distinct metric_name",
            "from lens_metrics_tags",
            "where app_id = #{appId,jdbcType=BIGINT}"
    })
    @ResultMap("lensMetricsTags")
    List<LensMetricsTags> selectMetricNameByAppId(@Param("appId") Long appId);

    @Select({
            "select",
            " distinct tag_key",
            "from lens_metrics_tags",
            "where app_id = #{appId,jdbcType=BIGINT} and metric_name = #{metricName,jdbcType=VARCHAR}"
    })
    @ResultMap("lensMetricsTags")
    List<LensMetricsTags> selectMetricTagKeyByAppId(@Param("appId") Long appId, @Param("metricName") String metricName);

    @Select({
            "select",
            " distinct tag_full",
            "from lens_metrics_tags",
            "where app_id = #{appId,jdbcType=BIGINT} and metric_name = #{metricName,jdbcType=VARCHAR}"
    })
    @ResultMap("lensMetricsTags")
    List<LensMetricsTags> selectMetricTagKeyValueByAppId(@Param("appId") Long appId, @Param("metricName") String metricName);


}
